package com.example.android.raycast;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

public class Weapon {
	
	//Weapon Types
	public static int WEAPON_SHOTGUN = 0;
	public static int WEAPON_RIFLE = 1;
	
	private int mWeaponType = 0;
	private int mRange = 30;
	private int mFullHitDamage = 10;
	private int mParialHitDamage = 5;
	
	private int mAmmo = 200;
	private int mCanvasWidth = 100;
	private int mCanvasHeight = 100;
	
    private static BitmapFactory.Options sBitmapOptions 
    = new BitmapFactory.Options();
    
    private Bitmap[] mBitmaps;
	private Rect mBitmapRect;
	private int mBitmapHeight;
	private int mBitmapWidth;
	
	public Weapon(int type, int canvasWidth, int canvasHeight, Context context) {
		mWeaponType = type;
		mCanvasWidth = canvasWidth;
		mCanvasHeight = canvasHeight;

		mBitmaps = new Bitmap[1];
		mBitmaps[0] = loadBitmap(context, R.drawable.gun2);
		mBitmapHeight = mBitmaps[0].getHeight();
		mBitmapWidth = mBitmaps[0].getWidth();

		//left top right bottom
		mBitmapRect = new Rect(mCanvasWidth - mBitmapWidth, mCanvasHeight- mBitmapHeight,mCanvasWidth,mCanvasHeight);
	}
	
	public void updateCanvasWidth(int canvasWidth, int canvasHeight) {
		mCanvasWidth = canvasWidth;
		mCanvasHeight = canvasHeight;
		mBitmapRect = new Rect(mCanvasWidth - mBitmapWidth, mCanvasHeight- mBitmapHeight,mCanvasWidth,mCanvasHeight);
	}
	
	public int getType() {
		return mWeaponType;
	}
	
	public int getRange() {
		return mRange;
	}
	
	public int getFullHitDamage() {
		return mFullHitDamage;
	}
	
	public int getPartialHitDamage() {
		return mParialHitDamage;
	}

	public void reduceAmmo() {
		mAmmo--;		
	}
	
	public int getStartFireHit() {
		return mCanvasWidth/2 - mRange;
	}
	
	public int getEndFireHit() {
		return mCanvasWidth/2 + mRange;
	}
	
	public void reset() {
		mAmmo = 100;	
	}
	
	public Rect getRect() {
		return mBitmapRect;
	}
	
    protected Bitmap loadBitmap(Context context, int resourceId) {
        Bitmap bitmap = null;
        if (context != null) {
          
            InputStream is = context.getResources().openRawResource(resourceId);
            try {
                bitmap = BitmapFactory.decodeStream(is, null, sBitmapOptions);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore.
                }
            }
        }

        return bitmap;
    }
    
	public Bitmap getCurrentBitmap() {		
		return mBitmaps[mWeaponType];
	}
	
	public void destroy() {
        for (int x = 0; x < mBitmaps.length; x++) {
        	if(mBitmaps[x] != null) {
        		mBitmaps[x].recycle();
        		mBitmaps[x] = null;
        	}
        }
	}

	public void ammoPickup() {
		mAmmo += 10;		
	}

	public int getAmmo() {
		return mAmmo;
	}
}
